from SOAPpy import WSDL
from configuration import Configuration

class Client:
    _server = None
    _conf = None
    
    def __init__(self, WSDLserver, configuration = Configuration()):
        self._server = WSDLserver
        if isinstance( configuration, Configuration):
            self._conf = configuration
    
    def setconfiguration(self, configuration):
        if isinstance( configuration, Configuration):
            self._conf = configuration
            
    def login(self):
        logged = self._server.auth(self._conf.mac, self._conf.email)
        if logged :
            print 'Logged.'
        else:
            print 'Problem in login operation!'
            print 'When try >> email', self._conf.email, ' mac ', self._conf.mac
      
        return logged
    
    def is_working_in_analysis(self):
        yetWorking = self._server.imWorkingIn(self._conf.mac, 
                                              self._conf.last_analysis_id)
        if yetWorking :
            print 'I am yet working in same analysis.'
        else:
            print 'I am NOT working in analysis with id ', 
            self._conf.last_analysis_id
    
        return yetWorking
    
    def is_analysis_done(self):
        done = self._server.analysisIsDone(self._conf.last_analysis_id)
        if done :
            print 'The analysis is done.'
        else :
            print 'The analysis is NOT done.'
      
        return done
    
    def clear_analysisdb(self):
        pass
    
    def _request_analysis_id(self):
        new_analysisId = self._server.requestIdAnalysis(self._conf.mac)
        print 'The id received is', new_analysisId
        
        return new_analysisId
    
    def send_clientdata(self):
        return False
        
    def take_new_work(self):
        self.clear_analysisdb()
        analysisId = self._request_analysis_id()
        self._conf.last_analysis_id = analysisId
    
        return self._conf
